from  mall.models.product import Product, ProductContent, ProductMetadata
from  mall.models.resource import Resource
from mall.models.metadata import Metadata
from mall.models.language import Language
import random


def init_test_data(session):
    session.query(Product).delete()
    session.query(ProductMetadata).delete()
    session.query(ProductContent).delete()
    resource_list = session.query(Resource).all()
    language_list = session.query(Language).all()
    metadata_list = session.query(Metadata).filter(Metadata.type == 'game')
    for i in range(0, 10):
        # 生成10个测试商品
        title = '测试商品-{}'.format(i)
        pid = id = 'p-{}'.format(i)

        product = Product(
            id=pid,
            title=title,
            sub_title='Sub Titme For = {}'.format(title),
            status='normal',
            minimum_price=0.00,
            maximum_price=100,
            average_price=i,
            stock=i * 3,
            cover_resource_id=random.choice(resource_list).id,
            category_id='game',
        )
        session.add(product)
        for metadata in metadata_list:
            if metadata.id == 'game-platform':
                if i <= 2:
                    # 头3个是PSN
                    product_metadata = ProductMetadata(
                        metadata_id=metadata.id,
                        product_id=pid,
                        name=metadata.screen_name,
                        value='PSN'
                    )
                    session.add(product_metadata)
                elif i > 2 and i <= 5:
                    # 3个是单Stream
                    product_metadata = ProductMetadata(
                        metadata_id=metadata.id,
                        product_id=pid,
                        name=metadata.screen_name,
                        value='Stream'
                    )
                    session.add(product_metadata)
                elif i > 5 and i <= 8:
                    # 3个是单XBOX
                    product_metadata = ProductMetadata(
                        metadata_id=metadata.id,
                        product_id=pid,
                        name=metadata.screen_name,
                        value='XBOX'
                    )
                else:
                    # 3个是所有平台
                    product_metadata = ProductMetadata(
                        metadata_id=metadata.id,
                        product_id=pid,
                        name=metadata.screen_name,
                        value='PSN,XBOX,Stream'
                    )
                    session.add(product_metadata)
            elif metadata.id == 'game-publish-date':
                product_metadata = ProductMetadata(
                    metadata_id=metadata.id,
                    product_id=pid,
                    name=metadata.screen_name,
                    value=str(2010 + i)
                )
                session.add(product_metadata)

        for language in language_list:
            product_content = ProductContent(
                summary='这是 {} {} 的摘要'.format(product.title, language.screen_name),
                detail='这是 {} {} 的详细'.format(product.title, language.screen_name),
                language_code = language.id,
                product_id = pid,
            )
            session.add(product_content)

    session.commit()